package yf.mall.service.impl;

import com.yf.service.impl.BaseServiceImpl;
import yf.mall.dao.UserDao;
import yf.mall.entity.UserEntity;
import yf.mall.vo.PriceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import yf.mall.dao.PriceDao;
import yf.mall.entity.PriceEntity;
import yf.mall.service.PriceService;

import javax.annotation.Resource;

/**
 * 代理价格Service实现类
 *
 * 
 *
 * @date Sun Sep 29 17:42:37 CST 2019
 */
@Service
public class PriceServiceImpl extends BaseServiceImpl<PriceEntity> implements PriceService {

    private PriceDao priceDao;


    @Autowired
    public void setDao(PriceDao priceDao) {
        this.priceDao = priceDao;
        super.setBaseDao(priceDao);
    }


    /**
     *
     *   @Override
     *     public List<UserVo> queryListVo(UserVo userVo) {
     *         return userDao.queryListVo(userVo);
     *     }
     *
     *     @Override
     *     public UserVo getVo(Long id) {
     *         return userDao.getVo(id);
     *     }

     @Override
     List<PriceVo> queryListVo(PriceVo priceVo){
            return priceDao.queryListVo(priceVo);
     }

     @Override
     PriceVo getVo(Long id){
                return priceDao.getVo(id);
    }

     **/


    /**内部方法**/



    /**外部方法实现**/

    @Override
    public List<PriceVo> findByGoodsIdAndCode(Long goodsId,String code) {
        List<PriceVo> priceVos = priceDao.findByGoodsIdAndCode(goodsId,code,null);
        if(priceVos.size()==0){ //不存在
            priceVos = priceDao.findByGoodsIdAndCode(goodsId,null,1);
            priceVos = priceVos.stream().filter(priceVo -> priceVo.getPrice().getDef()==1).collect(Collectors.toList());
        }
        return priceVos;
    }

    @Override
    public PriceEntity findByLevelIdAndGoodsIdAndCode(Long levelId,Long goodsId, String code) {
        PriceEntity priceEntity = priceDao.findByLevelIdAndGoodsIdAndCode(levelId,goodsId,code,null);
        if(priceEntity==null){
            priceEntity = priceDao.findByLevelIdAndGoodsIdAndCode(levelId,goodsId,null,1);
        }
        return priceEntity;
    }

    @Override
    public PriceEntity findByUserIdAndGoodsIdAndCode(Long userId, Long goodsId,String code) {
        PriceEntity priceEntity = priceDao.findByUserIdAndGoodsIdAndCode(userId,goodsId,code,null);
        if(priceEntity==null){
            priceEntity = priceDao.findByUserIdAndGoodsIdAndCode(userId,goodsId,null,1);
        }
        return priceEntity;
    }

    @Resource
    private UserDao userDao;

    @Override
    public PriceEntity findByUserIdAndGoodsId(Long userId, Long goodsId) {
        UserEntity user = userDao.get(userId);
        return findByUserIdAndGoodsIdAndCode(userId,goodsId,user.getTeamCode());
    }

    @Override
    public void deleteNotIn(List<Long> ids, Long goodsId) {
         priceDao.deleteNotIn(ids,goodsId);
    }
}
